#include<stdio.h>
#include<time.h>
#include<unistd.h>
#include<string.h>

int main(){
	FILE *fp;
	time_t ctime;
	struct tm * ctimestr;
	int linecount = 0;
	char buf[32];
	fp = fopen("1.txt", "a+");
	if(fp == NULL){
		perror("fopen");
		return 0;
	}

	//获取现有行号
	while(fgets(buf, 32, fp) != NULL){
		if(buf[strlen(buf) - 1] == '\n'){
			linecount++;
		}
	}

	while(1){
		ctime = time(NULL);
		ctimestr = localtime(&ctime);
		printf("%04d-%02d-%02d %02d:%02d:%02d\n", ctimestr->tm_year + 1900, ctimestr->tm_mon + 1, ctimestr->tm_mday, ctimestr->tm_hour, ctimestr->tm_min, ctimestr->tm_sec);
		fprintf(fp, "%d, %04d-%02d-%02d %02d:%02d:%02d\n", linecount, ctimestr->tm_year + 1900, ctimestr->tm_mon + 1, ctimestr->tm_mday, ctimestr->tm_hour, ctimestr->tm_min, ctimestr->tm_sec);
		fflush(fp);
		linecount++;
		sleep(1);
	}

	fclose(fp);


}
